package com.kaifamiao.www.demo.do0805;

import java.util.Arrays;

public class identifyUtil {
    public static String[] KEYWORDS = {
            "abstract", "continue", "for", "new", "switch",
            "assert", "default", "if", "package", "synchronized",
            "boolean", "do", "goto", "private", "this",
            "break", "double", "implements", "protected", "throw",
            "byte", "else", "import", "public", "throws",
            "case", "enum", "instanceof", "return", "transient",
            "catch", "extends", "int", "short", "try",
            "char", "final", "interface", "static", "void",
            "class", "finally", "long", "strictfp", "volatile",
            "const", "float", "native", "super", "while",
            "_"
    };
    static String[] EXCLUDE = {"true","false","null"};

    //排序
    static {
        Arrays.sort(KEYWORDS);
        Arrays.sort(EXCLUDE);
    }
    /**

     * 是否符合标识符命名规则
     * @param str
     * @return
     */
    public boolean isIdentify(String str){
        if(str==null||str.isBlank()){
            return false;
        }
        //判断是否是关键字
        int index = Arrays.binarySearch(KEYWORDS,str);
        if(index < 0){
            //判断是否是特殊值
            index = Arrays.binarySearch(EXCLUDE,str);
            if (index < 0){
                //数字不能开头
                //str.charAt(0)
                String first = str.substring(0,1);
                if(!first.matches("\\d")){
                    //第一位不是数字
                    str = str.replace("$","");//去掉“$”符号
                    str = str.replaceAll("\\w","");//去掉数字和字母
                    if(str.length()==0){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**

     * 是否是 使用骆驼命名规则 的标识符
     * 首字母小写
     * 多个单词使用首字母大写的方式拼接(如果是使用 _ 连接则不符合要求)
     * @param str 标识符
     * @return
     *
     * girl_friend false
     * Girl    false
     *
     */
    public boolean isCamel(String str){
        return false;
    }


    /**
     * 是否是 使用帕斯卡命名规则 的标识符
     *      首字母大写
     *      多个单词使用首字母大写的方式拼接(如果是使用 _ 连接则不符合要求)
     * @param str
     * @return
     *
     * GirlFriend true
     * Girl_Friend  false
     */
    public boolean isPascal(String str){
        return false;
    }

    /**
     * 将 标识符 转换为 骆驼命名规则的标识符
     * @param str  girl_friend
     * @return  girlFriend
     */
    public String translateCamelWords(String str){
        if (isIdentify(str)){
            //先全部转小写
            str = str.toLowerCase();
            //去掉首位下划线
            str = str.replaceAll("^_*","");
            str = str.replaceAll("_*$","");
            int index = -1;
            while ((index = str.indexOf("_"))!=-1){
             String s =str.substring(index+1,index+2);
             str = str.replaceFirst("_"+s,s.toUpperCase());
            }
            return str;
        }
        return null;
    }
    /**
     * 将 标识符 转换为 帕斯卡命名规则的标识符
     * @param str  girl_friend
     * @return  GirlFriend
     */
    public String translatePascalWords(String str){
        str = str.toLowerCase();
        str = str.replaceAll("^_*","");
        str = str.replaceAll("_*$","");


        return null;
    }

}

